package com.glela.common.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

public class Base64Util {

	/**
	 * @param bytes
	 * @return
	 */
	public static String decode(final String string) {
		try {
			return new String(Base64.decodeBase64(string.getBytes("UTF-8")), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 二进制数据编码为BASE64字符串
	 * 
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	public static String encode(final String string) {
		try {
			return new String(Base64.encodeBase64(string.getBytes("UTF-8")), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		String string = "{"+
    "\"address\": \"上海南\","+
    "\"area\": \"\","+
    "\"baUserId\": 0,"+
    "\"billHeader\": \"我的发票\","+
    "\"billType\": 1,"+
    "\"cashCoin\": 0,"+
    "\"city\": \"\","+
    "\"deliveryWay\": 0,"+
    "\"isBill\": 1,"+
    "\"linkMan\": \"毛泽尧\","+
    "\"mallStoreId\": 0,"+
    "\"mobile\": \"18827070700\","+
    "\"orderDetailList\": ["+
    "{"+
    "\"activityType\": 0,"+
    "\"afterIsRefund\": 0,"+
    "\"afterReason\": \"\","+
    "\"afterSalesId\": 0,"+
    "\"afterStatus\": 5,"+
    "\"afterType\": 0,"+
    "\"apiGoodsId\": 0,"+
    "\"apiOrderDetailId\": 0,"+
    "\"applywelfareId\": 0,"+
    "\"backtrackCash\": 0,"+
    "\"brandId\": 98,"+
    "\"brandName\": \"吉衣贡\","+
    "\"channel\": 0,"+
    "\"commentStatus\": 0,"+
    "\"deliveryWay\": 1,"+
    "\"goodId\": 75,"+
    "\"goodName\": \"吉衣贡男士长袖衬衫\","+
    "\"groupBuyingId\": 0,"+
    "\"inventoryId\": 91,"+
    "\"isAfterSale\": 0,"+
    "\"mallStoreId\": 0,"+
    "\"navigateImg1\": \"images/goodsSpecImg/201612121138178336.jpg\","+
    "\"productId\": 68,"+
    "\"productPrice\": 13800,"+
    "\"salePrice\":8888,"+
    "\"quantity\": 1,"+
    "\"shareActivityId\": 0,"+
    "\"shareSerialId\": 1,"+
    "\"specName\": \"GAS109017302 S\","+
    "\"storeId\": 20,"+
    "\"talentId\": 510,"+
    "\"talentSerialEditandshareId\": 0,"+
    "\"talentSerialId\": 0,"+
    "\"talentShareGoodId\": 0,"+
    "\"type\": 0,"+
    "\"userId\": 0"+
    "}"+
    "],"+
    "\"payType\": 1,"+
    "\"promotionId\": 0,"+
    "\"province\": \"上海市\","+
    "\"remark\": \"快点送来\","+
    "\"roleId\": 0,"+
    "\"storeId\": 0,"+
    "\"userId\": 0"+
    "}";
		System.out.println(encode(string));
		String string2="eyJhZGRyZXNzIjoi5LiK5rW35Y2XIiwiYXJlYSI6IiIsImJhVXNlcklkIjowLCJiaWxsSGVhZGVyIjoi5oiR55qE5Y+R56WoIiwiYmlsbFR5cGUiOjEsImNhc2hDb2luIjowLCJjaXR5IjoiIiwiZGVsaXZlcnlXYXkiOjAsImlzQmlsbCI6MSwibGlua01hbiI6Iuavm+azveWwpyIsIm1hbGxTdG9yZUlkIjowLCJtb2JpbGUiOiIxODgyNzA3MDcwMCIsIm9yZGVyRGV0YWlsTGlzdCI6W3siYWN0aXZpdHlUeXBlIjowLCJhZnRlcklzUmVmdW5kIjowLCJhZnRlclJlYXNvbiI6IiIsImFmdGVyU2FsZXNJZCI6MCwiYWZ0ZXJTdGF0dXMiOjUsImFmdGVyVHlwZSI6MCwiYXBpR29vZHNJZCI6MCwiYXBpT3JkZXJEZXRhaWxJZCI6MCwiYXBwSWQiOjAsImFwcGx5d2VsZmFyZUlkIjowLCJiYWNrdHJhY2tDYXNoIjowLCJicmFuZElkIjo5OCwiYnJhbmROYW1lIjoi5ZCJ6KGj6LShIiwiY2hhbm5lbCI6MCwiY29tbWVudFN0YXR1cyI6MCwiZGVsaXZlcnlXYXkiOjEsImdvb2RJZCI6NzUsImdvb2ROYW1lIjoi5ZCJ6KGj6LSh55S35aOr6ZW/6KKW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